<?php
/**
 * 时间段模型
 * @author jianhui
 *
 */
class timeUnitModel extends MY_Model{
    function timeUnitModel(){
        parent::MY_Model();
        $this->table = 'TimeUnit';
        $this->load->library('Cache');
    }
    /**
     * 缓存时间配置
     */
    function createTimeUnitCache(){
        $yearList = array();//年份列表
        $timeUnitList = array();//时段列表
        $timeUnitDataList = array();//按年排列时段列表
        $findList = $this->getList(array('id > '=>0, 'startTime > ' => '2015-06-30 23:59:59'), null, null, '', 'year asc,orderNum asc');
        foreach ($findList as $v){
            //年份列表
            isset($yearList[(int)$v['year']]) || $yearList[(int)$v['year']] = (int)$v['year'].'年';
            //时段列表
            isset($timeUnitList[(int)$v['year']]) || $timeUnitList[(int)$v['year']] = array();
            $timeUnitList[(int)$v['year']][(int)$v['id']] = $v['name'];
            //时段数据列表
            isset($timeUnitDataList[(int)$v['year']]) || $timeUnitDataList[(int)$v['year']] = array();
            $v['startTime'] = strtotime($v['startTime']);
            $v['endTime'] = strtotime($v['endTime']);
            $v['singleBudgetApplyStartTime'] = strtotime($v['singleBudgetApplyStartTime']);
            $v['singleBudgetApplyEndTime'] = strtotime($v['singleBudgetApplyEndTime']);
            $v['singleBudgetAuditStartTime'] = strtotime($v['singleBudgetAuditStartTime']);
            $v['singleBudgetAuditEndTime'] = strtotime($v['singleBudgetAuditEndTime']);
            $v['singleSettleApplyStartTime'] = strtotime($v['singleSettleApplyStartTime']);
            $v['singleSettleApplyEndTime'] = strtotime($v['singleSettleApplyEndTime']);
            $v['singleSettleAuditStartTime'] = strtotime($v['singleSettleAuditStartTime']);
            $v['singleSettleAuditEndTime'] = strtotime($v['singleSettleAuditEndTime']);
            $v['singleSettleScoreStartTime'] = strtotime($v['singleSettleScoreStartTime']);
            $v['singleSettleScoreEndTime'] = strtotime($v['singleSettleScoreEndTime']);
            $v['singleSettleChangeStartTime'] = strtotime($v['singleSettleChangeStartTime']);
            $v['singleSettleChangeEndTime'] = strtotime($v['singleSettleChangeEndTime']);
            $timeUnitDataList[(int)$v['year']][(int)$v['id']] = $v;
        }
        $this->cache->set('yearList', $yearList);//缓存年份列表
        $this->cache->set('timeUnitList', $timeUnitList);//缓存时段列表
        $this->cache->set('timeUnitDataList', $timeUnitDataList);//缓存时段数据
    }
    /**
     * 获取时段相关数据
     * @param string $type
     * yearList：年份列表
     * timeUnitList：时段列表
     * timeUnitDataList：时段数据
     * @return array
     */
    function getCacheData($type){
        $this->cache->expire = 86400;//设置过期时间为一天
        $data = $this->cache->get($type);
        if (!$data){
            //创建缓存
            $this->createTimeUnitCache();
            $data = $this->cache->get($type);
        }
        return $data;
    }
    /**
     * 获取当前时段ID
     * @return Ambigous <>
     */
    function getNowTimeUnit(){
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        $list = isset($timeUnitDataList[date('Y')]) ? $timeUnitDataList[date('Y')] : array();
        if ($list){
            $nowTime = time();
            foreach($list as $k => $v){
                if ($nowTime > $v['startTime'] && $nowTime < $v['endTime']){
                    return $v['id'];
                }
            }
        }
    }
    /**
     * 获取时段列表
     * @param number $year
     * @return Ambigous <multitype:, array>
     */
    function getTimeUnitList($year = 2015){
        $timeUnitList = $this->getCacheData('timeUnitList');
        return isset($timeUnitList[$year]) ? $timeUnitList[$year] : array();
    }
    /**
     * 获取指定时段开始时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     */
    function getTimeUnitStartTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['startTime'] : time();
    }
    /**
     * 获取指定时段开始日期
     * @param int $timeUnit
     * @param int $year
     * @return string
     */
    function getTimeUnitStartDate($timeUnit, $year){
        $startTime = $this->getTimeUnitStartTime($timeUnit, $year);
        return date("Y-m-d", $startTime);
    }
    /**
     * 获取指定时段结束时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return int
     */
    function getTimeUnitEndTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['endTime'] : time();
    }
    /**
     * 获取指定时段结束日期
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return string
     */
    function getTimeUnitEndDate($timeUnit, $year){
        $endTime = $this->getTimeUnitEndTime($timeUnit, $year);
        return date("Y-m-d", $endTime);
    }
    
}